package com.jiangman.weather.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jiangman.weather.BaseApplication;
import com.jiangman.weather.R;
import com.jiangman.weather.base.BaseFragment;
import com.jiangman.weather.bean.ConstantMsg;
import com.jiangman.weather.bean.MessageEvent;
import com.jiangman.weather.ui.activity.ChangPwdActivity;
import com.jiangman.weather.ui.activity.CityManageActivity;
import com.jiangman.weather.ui.activity.LoginActivity;
import com.jiangman.weather.ui.activity.SetIpActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import util.HttpRequestUtils;
import util.ToastUitl;

/**
 * Created by secoro on 2017/4/26.
 */

public class MeFragment extends BaseFragment {

    @BindView(R.id.ll_login)
    LinearLayout llLogin;
    @BindView(R.id.me_chang_pwd)
    RelativeLayout meChangPwd;
    @BindView(R.id.about_us)
    RelativeLayout aboutUs;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.set_ip)
    RelativeLayout setIp;
    @BindView(R.id.me_city_manage)
    RelativeLayout meCityManage;
    @BindView(R.id.me_city_syn)
    RelativeLayout meCitySyn;
    private HttpRequestUtils httpRequestUtils;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_me, null);
        ButterKnife.bind(this, inflate);
        httpRequestUtils = new HttpRequestUtils(getActivity());
        return inflate;
    }

    @OnClick({R.id.ll_login, R.id.me_chang_pwd, R.id.about_us})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_login:
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.me_chang_pwd:
                startActivity(new Intent(getActivity(), ChangPwdActivity.class));
                break;
            case R.id.about_us:
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {

        if (event.message_id == ConstantMsg.update_user) {
            System.out.println("收到消息");
            userName.setText(event.user_name);
        }

    }


    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();

    }


    @OnClick(R.id.set_ip)
    public void onClick() {
        startActivity(new Intent(getActivity(), SetIpActivity.class));
    }

    @OnClick(R.id.me_city_manage)
    public void onClick2() {
        startActivity(new Intent(getActivity(), CityManageActivity.class));
    }


    @OnClick(R.id.me_city_syn)
    public void onClick3() {
        if(BaseApplication.isLogin){
            ToastUitl.show("正在同步", Toast.LENGTH_SHORT);
            httpRequestUtils.selectAllCity();
        }else {
            ToastUitl.show("请先登录账号", Toast.LENGTH_SHORT);
        }

    }


}
